#include "thresholdform.h"
#include "ui_thresholdform.h"
#include <QPainter>
#include <QStyleOption>
#include <QPushButton>

ThresholdForm::ThresholdForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ThresholdForm)
{
    ui->setupUi(this);
    this->setProperty("form","backgroundform");
    ui->leftBorderForm->SetLabelIcon("threshold");
    this->setFixedHeight(100);
    
    // 连接doubleSpinBox的valueChanged信号到槽函数
    connect(ui->doubleSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &ThresholdForm::onDoubleSpinBoxValueChanged);
    
    // 连接widget_2中按钮的clicked信号到槽函数
    connect(ui->widget_2->findChild<QPushButton*>("pushButton_level1"), &QPushButton::clicked,
            this, &ThresholdForm::onPushButtonLevel1Clicked);
    connect(ui->widget_2->findChild<QPushButton*>("pushButton_level2"), &QPushButton::clicked,
            this, &ThresholdForm::onPushButtonLevel2Clicked);
    connect(ui->widget_2->findChild<QPushButton*>("pushButton_level3"), &QPushButton::clicked,
            this, &ThresholdForm::onPushButtonLevel3Clicked);
    
    // 初始化时根据当前值设置按钮状态
    onDoubleSpinBoxValueChanged(ui->doubleSpinBox->value());
}

void ThresholdForm::onDoubleSpinBoxValueChanged(double value)
{
    // 转换为整数进行比较
    int intValue = static_cast<int>(value);
    
    // 直接访问widget_2中的按钮
    QPushButton *level1 = ui->widget_2->findChild<QPushButton*>("pushButton_level1");
    QPushButton *level2 = ui->widget_2->findChild<QPushButton*>("pushButton_level2");
    QPushButton *level3 = ui->widget_2->findChild<QPushButton*>("pushButton_level3");
    
    if (level1 && level2 && level3) {
        if (intValue == 0)
        {
            // 值为0时，都不选中
            level1->setChecked(false);
            level2->setChecked(false);
            level3->setChecked(false);
        }
        else if (intValue == 1) {
            // 值为1时，仅最左边选中
            level1->setChecked(true);
            level2->setChecked(false);
            level3->setChecked(false);
        } else if (intValue >= 2 && intValue <= 4) {
            // 值为2-4时，最左边和中间选中
            level1->setChecked(true);
            level2->setChecked(true);
            level3->setChecked(false);
        } else if (intValue >= 5 && intValue <= 100) {
            // 值为5-100时，三个都选中
            level1->setChecked(true);
            level2->setChecked(true);
            level3->setChecked(true);
        }
    }
}

void ThresholdForm::onPushButtonLevel1Clicked(bool checked)
{
    // 获取所有按钮的状态
    QPushButton *level1 = ui->widget_2->findChild<QPushButton*>("pushButton_level1");
    QPushButton *level2 = ui->widget_2->findChild<QPushButton*>("pushButton_level2");
    QPushButton *level3 = ui->widget_2->findChild<QPushButton*>("pushButton_level3");
    
    if (level1 && level2 && level3) {
        // 断开doubleSpinBox的信号连接，避免循环更新
        disconnect(ui->doubleSpinBox, nullptr, this, nullptr);
        
        if (level1->isChecked() && !level2->isChecked() && !level3->isChecked()) {
            // 只有最左边选中，设置为1
            ui->doubleSpinBox->setValue(1);
        }
        
        // 重新连接信号
        connect(ui->doubleSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
                this, &ThresholdForm::onDoubleSpinBoxValueChanged);
    }
}

void ThresholdForm::onPushButtonLevel2Clicked(bool checked)
{
    // 获取所有按钮的状态
    QPushButton *level1 = ui->widget_2->findChild<QPushButton*>("pushButton_level1");
    QPushButton *level2 = ui->widget_2->findChild<QPushButton*>("pushButton_level2");
    QPushButton *level3 = ui->widget_2->findChild<QPushButton*>("pushButton_level3");
    
    if (level1 && level2 && level3) {
        // 断开doubleSpinBox的信号连接，避免循环更新
        disconnect(ui->doubleSpinBox, nullptr, this, nullptr);
        
        if (level1->isChecked() && level2->isChecked() && !level3->isChecked()) {
            // 最左边和中间选中，设置为3
            ui->doubleSpinBox->setValue(3);
        } else if (level1->isChecked() && !level2->isChecked() && !level3->isChecked()) {
            // 只有最左边选中，设置为1
            ui->doubleSpinBox->setValue(1);
        }else if (!level1->isChecked() && !level2->isChecked() && !level3->isChecked()) {
            // 只有最左边选中，设置为1
            ui->doubleSpinBox->setValue(0);
        }
        
        // 重新连接信号
        connect(ui->doubleSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
                this, &ThresholdForm::onDoubleSpinBoxValueChanged);
    }
}

void ThresholdForm::onPushButtonLevel3Clicked(bool checked)
{
    // 获取所有按钮的状态
    QPushButton *level1 = ui->widget_2->findChild<QPushButton*>("pushButton_level1");
    QPushButton *level2 = ui->widget_2->findChild<QPushButton*>("pushButton_level2");
    QPushButton *level3 = ui->widget_2->findChild<QPushButton*>("pushButton_level3");
    
    if (level1 && level2 && level3) {
        // 断开doubleSpinBox的信号连接，避免循环更新
        disconnect(ui->doubleSpinBox, nullptr, this, nullptr);
        
        if (level1->isChecked() && level2->isChecked() && level3->isChecked()) {
            // 三个都选中，设置为5
            ui->doubleSpinBox->setValue(5);
        } else if (level1->isChecked() && level2->isChecked() && !level3->isChecked()) {
            // 最左边和中间选中，设置为3
            ui->doubleSpinBox->setValue(3);
        }
        
        // 重新连接信号
        connect(ui->doubleSpinBox, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
                this, &ThresholdForm::onDoubleSpinBoxValueChanged);
    }
}

ThresholdForm::~ThresholdForm()
{
    delete ui;
}

void ThresholdForm::UpdateShowType(int type)
{
    ui->leftBorderForm->UpdateShowType(type);
}

void ThresholdForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void ThresholdForm::on_toolButton_contractExpand_clicked(bool checked)
{
    ui->widget_hide->setHidden(!checked);
    if(checked)
        this->setFixedHeight(100);
    else
        this->setFixedHeight(50);
}
